Return to doc.sitecore.com

Documentation

Sort By Field control allows to sort Items by the field values of the Data section. The control implements a modal dialog which allows choosing the data to sort by (sort by numbers, text or ISO date time values), and the sorting direction.

1.  Installation

The Sort By Field Control is distributed as a standard Sitecore package. Thus to start using it, you should install the package. Please refer to the Installing Modules and Packages article if you are not familiar with the standard Sitecore Packager tool. 

The package adds the following Items:

Add the following lines to the web.config file under the <controlSources> tag:

<source mode="on" namespace="Sitecore.Shell.Applications.Dialogs" folder="/sitecore modules/shell/sorting" deep="true" prefix="sorting" />

<source mode="on" namespace="Sitecore.Shell.Applications.Dialogs" assembly="SortByField" prefix="sorting" />

And the line below under the <ui><references> section:

<reference>/bin/sortbyfield.dll</reference>

Perform the step below in order to add the Sort Items by Field button to the Sitecore Toolbar.

Switch to core database. Go to /sitecore/content/system/shell/__default/commands and add the Sort By Field command to the multilist selection:

/upload/sdn5/shared library/shell extensions/sort by field/sort_by_field_00.png 

Save the Commands Item.

Now you should see the Sort Items by Field button on the Toolbar:

/upload/sdn5/shared library/shell extensions/sort by field/sort_by_field_01.png 

Note: the button is only visible if the context Item has children.

 

2.  User Manual

Select an Item with children and click the Sort Items by Field button. A modal dialog which allows different sorting options will appear:

/upload/sdn5/shared library/shell extensions/sort by field/sort_by_field_02.png

Select field

Choose a field by which the Items will be sorted. The list consists of all fields enumerated in the Sitecore.FieldIDs (except those belonging to the “System” section) of the Item on which the sorting is invoked. All user defined template fields are also added to the list.

Select sorting type

This setting defines how the values of the fields will be treated: as text, as numbers or as ISODateTime values.

Sorting direction

Sorting Direction allows selecting between 2 possible directions of sorting: ascending or descending.

After pressing the Sort button, all sibling Items will be sorted by chosen criteria.

3.  Custom Comparers (Architectural Notes)

The module installs 3 predefined value types: ISODateTime, Number, Text. However, it is possible to extend this list. To do that a user should add a new Item under system/Modules/Sorting using the SortingData template installed by the package.

/upload/sdn5/shared library/shell extensions/sort by field/sort_by_field_03.png

The type field points to a class which handles the sorting.

3.1.  Comparer Class

The class is used for comparing field values.

It is obligatory that this class derives from the Comparer abstract class and implements the following methods:

      public abstract bool IsFieldValueValid(string fieldValue);
  
public abstract int Compare(object x, object y);
Please look at the snippet which shows the realization of the Sorting.Comparer
class:
public abstract class Comparer: IComparer  
   {
      
#region Fields
      
bool isAscending; //true if ascending sorting is used, false otherwise
      string fieldName; //name of the field to sort on
      #endregion

      
#region Properties
      
public bool IsAscending
      {
        
get
         {
            
return isAscending;
         }
        
set
         {
            isAscending  
= value;
         }
      }

      
public string FieldName
      {
        
get
         {
            
return fieldName;
         }
        
set
         {
            fieldName
= value;
         }
      }
      
#endregion

      
#region abstract
      
public abstract bool IsFieldValueValid(string fieldValue);
      
public abstract int Compare(object x, object y);
      
#endregion

      
#region Constructors
      
public Comparer(bool isAscending, string fieldName)
      {
        
this.isAscending = isAscending;
        
this.fieldName = fieldName;
      }
      
#endregion
   }

3.2.  TextComparer Class

The TextComparer class itself is shown below:

public class TextComparer: Comparer
   {        
      
//returns true if fieldValue format corresponds to the class data type
      
//false – otherwise.
      public override bool IsFieldValueValid(string fieldvalue)
      {
        
return true;
      }

      
//It is necessary to call base class constructor this way:
      public TextComparer(bool isAscending, string fieldName): base(isAscending, fieldName)
      {                                  
      }

    
      
#region IComparer Members
      
//compare 2 field values:
      
//0  – values equal
      
//1  – first value > than second value
      
//-1 – second value > than first value
      public override int Compare(object x, object y)
      {      
         Item itemX
= x as Item;
         Item itemY
= y as Item;
        
string fieldValueX = itemX[FieldName];
        
string fieldValueY = itemY[FieldName];
        
        
if(IsAscending)
         {
            
return string.Compare(fieldValueX, fieldValueY);
         }
        
else
         {
            
return string.Compare(fieldValueY, fieldValueX);
         }
      }

      
#endregion
   }

It is necessary to have the “write” permission for an Item in order to implement the sorting. Otherwise, it is impossible to change the “sort order”.

If a User does not have the "write" permission for one of the Items to be sorted, it will be sorted anyway, being placed at the end of the list. The same will happen if the Item does not contain the field by which the sorting is performed.

Please note: if more than one User sorts the same Items simultaneously, the final order might be unpredictable. Also please note that if a User sorts by the  __Display Name field, it is not always the same as sorting by item.DisplayName.